﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Form_Form_Team : System.Web.UI.Page
{
    #region 公共变量

    string[] arrField = { "TeamId", "TeamName" };

    #endregion

    #region 初始化

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {

            dg_manage.Style.Add("overflow", "auto");
            dg_manage.Style.Add("height", "600px");
            dg_manage.Style.Add("width", "100%");
            SysCommon.SelectNull(this);            //若未选中任何行，客户端的提示
            //当前选定行在客户端记录
            Formspan.InnerHtml = ("<script language=javascript>var CurrentselectRow=" + (this.DGV.SelectedIndex + ";</script>"));  //span里面加HTML标记,刷新之后还保留

            DataBing_DGV();

            LTitle.Text = "组信息";
        }
    }
    #endregion

    #region 数据绑定

    //数据绑定
    private void DataBing_DGV()
    {
        DataTable dt = DataBaseOperate_SQLServer.GetDataTable(("select TeamId,TeamName from t_team"));
        DataView dv = dt.DefaultView;
        //  dv.RowFilter = ViewState("RF")
        if ((dv.Count == 0))
        {
            DataRowView d = dv.AddNew();
            DGV.DataSource = dv;
            DGV.DataBind();
            Int32 columnCount = DGV.Rows[0].Cells.Count;
            DGV.Rows[0].Cells.Clear();
            DGV.Rows[0].Cells.Add(new TableCell());
            DGV.Rows[0].Cells[0].ColumnSpan = columnCount;
            DGV.Rows[0].Cells[0].Text = "No Records Found.";
            DGV.Rows[0].Visible = false;
        }
        else
        {
            DGV.DataSource = dv;
            int PageMax = (int)Math.Ceiling((double)(dv.Count / DGV.PageSize)) - 1;
            if ((DGV.PageIndex > PageMax))
            {
                DGV.PageIndex = 0;
            }
            DGV.DataBind();
        }
        PAN1.Visible = false;
        DGV.Visible = true;
    }
    #endregion

    #region GridView事件

    protected void DGV_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        if (ViewState["selectRow"] != null)
        {
            if (((int)ViewState["selectRow"] == ((GridView)(sender)).SelectedIndex))
            {
                ((GridView)(sender)).SelectedIndex = -1;
            }
        }
        ViewState["selectRow"] = ((GridView)(sender)).SelectedIndex;
        Formspan.InnerHtml = ("<script language=javascript>var CurrentselectRow=" + (this.DGV.SelectedIndex + ";</script>"));
    }

    protected void DGV_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        DGV.PageIndex = e.NewPageIndex;
        DataBing_DGV();
    }
    #endregion

    #region 新增按钮

    protected void BtnAdd_Click(object sender, System.EventArgs e)
    {
        PAN1.Visible = true;
        DGV.Visible = false;
        SysCommon.ResetControl(PAN1);
        ViewState["edit"] = false;
        LblPanTitle.Text = "新 增";
        FormBottom.Controls.Add(new LiteralControl("<script language=javascript>document.Form1.TxtTeamName.focus();</script>"));
    }
    #endregion

    #region 修改按钮

    protected void BtnModify_Click(object sender, System.EventArgs e)
    {
        if (((DGV.SelectedIndex == -1) || (DGV.SelectedIndex >= DGV.Rows.Count)))
        {
            SysCommon.JavaScript.alert("请选择需要修改的数据！");
        }
        else
        {
            PAN1.Visible = true;
            DGV.Visible = false;
            Int32 i = int.Parse(((System.Web.UI.HtmlControls.HtmlInputHidden)(DGV.SelectedRow.Cells[1].FindControl("hiddenId"))).Value);
            DataRow dr = DataBaseOperate_SQLServer.GetDataRow("select * from t_team where TeamId=" + i);
            for (i = 0; i < arrField.Length; i++)
            {
                if (dr[arrField[i]] != null)
                {
                    if ((PAN1.FindControl("Txt" + arrField[i]) is TextBox))
                    {
                        ((TextBox)(PAN1.FindControl("Txt" + arrField[i]))).Text = (string)dr[arrField[i]];
                    }
                    else if ((PAN1.FindControl("Ddl" + arrField[i]) is DropDownList))
                    {
                        ((DropDownList)(PAN1.FindControl("Ddl" + arrField[i]))).SelectedIndex = -1;
                        ((DropDownList)(PAN1.FindControl("Ddl" + arrField[i]))).SelectedValue = dr[arrField[i]].ToString();
                    }
                }
            }
            ViewState["edit"] = true;
            LblPanTitle.Text = "修 改";
            FormBottom.Controls.Add(new LiteralControl("<script language=javascript>document.Form1.TxtTeamName.focus();</script>"));
        }
    }
    #endregion

    #region 删除按钮

    protected void BtnDelete_Click(object sender, System.EventArgs e)
    {
        if (((DGV.SelectedIndex == -1) || (DGV.SelectedIndex >= DGV.Rows.Count)))
        {
            SysCommon.JavaScript.alert("请选择相应的数据！");
        }
        else
        {
            DataBaseOperate_SQLServer.ExecUpdate(("delete from t_team where TeamId=" + int.Parse(((System.Web.UI.HtmlControls.HtmlInputHidden)(DGV.SelectedRow.Cells[1].FindControl("hiddenId"))).Value)));
            DataBing_DGV();
        }
    }
    #endregion

    #region 保存按钮

    protected void BtnSave_Click(object sender, System.EventArgs e)
    {
        SqlCommand objCommand = new SqlCommand();
        objCommand.Connection = DataBaseOperate_SQLServer.objConnection;

        try
        {
            System.Text.StringBuilder sbstr = new System.Text.StringBuilder(200);

            if ((bool)ViewState["edit"] == false)
            {
                //新增

                sbstr.Append("insert into t_team(");
                for (int i = 1; i < arrField.Length; i++)
                {
                    sbstr.Append(arrField[i]);
                    if (i < arrField.Length - 1)
                    {
                        sbstr.Append(",");
                    }
                }
                sbstr.Append(") values (");
                for (int i = 1; i < arrField.Length; i++)
                {
                    sbstr.Append("@");
                    sbstr.Append(arrField[i]);
                    if (i < arrField.Length - 1)
                    {
                        sbstr.Append(",");
                    }
                }

                sbstr.Append(")");
            }
            else
            {
                //修改

                sbstr.Append("update t_team set ");
                for (int i = 1; i < arrField.Length; i++)
                {
                    sbstr.Append(arrField[i]);
                    sbstr.Append("=");
                    sbstr.Append("@");
                    sbstr.Append(arrField[i]);

                    if (i < arrField.Length - 1)
                    {
                        sbstr.Append(",");
                    }
                }
                sbstr.Append(" where TeamId=");
                sbstr.Append(((System.Web.UI.HtmlControls.HtmlInputHidden)(DGV.SelectedRow.Cells[1].FindControl("hiddenId"))).Value);

                ViewState["edit"] = false;
            }

            objCommand.CommandText = sbstr.ToString();

            SqlParameter tempParameter;

            for (int i = 1; i < arrField.Length; i++)
            {

                if ((PAN1.FindControl("Txt" + arrField[i]) is TextBox))
                {
                    tempParameter = new SqlParameter("@" + arrField[i], SqlDbType.VarChar);
                    tempParameter.Value = ((TextBox)(PAN1.FindControl("Txt" + arrField[i]))).Text;
                    objCommand.Parameters.Add(tempParameter);
                }
                else if ((PAN1.FindControl("Ddl" + arrField[i]) is DropDownList))
                {
                    objCommand.Parameters.Add("@" + arrField[i], ((DropDownList)(PAN1.FindControl("Ddl" + arrField[i]))).SelectedValue);
                }
                else if ((PAN1.FindControl("Chk" + arrField[i]) is CheckBox))
                {
                    objCommand.Parameters.Add("@" + arrField[i], ((CheckBox)(PAN1.FindControl("Chk" + arrField[i]))).Checked == true ? 1 : 0);
                }
            }
            DataBaseOperate_SQLServer.ExecUpdate(objCommand);

        }
        catch (Exception ex)
        {
            Log.Record(ex);
        }
        finally
        {
            DataBing_DGV();
        }
    }
    #endregion

    #region 取消按钮

    protected void BtnCancel_Click(object sender, System.EventArgs e)
    {
        PAN1.Visible = false;
        DGV.Visible = true;
    }

    #endregion

}

